package com.langsin.crmpro.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.langsin.crmpro.base.BaseDao;
import com.langsin.crmpro.model.SDictionaryDetail;

public class DictionaryDetailDaoImpl extends BaseDao implements DictionaryDetailDAO{

	private static final long serialVersionUID = 1386598549691948064L;

	public void updateDictionaryDetails(final String id, final String  value) 
	{
		this.getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException
			{
				Query query = session.createSQLQuery(" update s_dictionary_detail set value=:value where id=:id");
				query.setParameter("value", value);
				query.setParameter("id", id);
				int count = query.executeUpdate();
				
				if(count > 0)
					log.debug("updateDictionaryDetails success");
				else
					log.debug("updateDictionaryDetails fail");
				
				return count;
			}
			
		});
	}
	
	public Object findUpSort(final Integer sort, final String  code) 
	{
		return this.getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException
			{
				Query query = session.createSQLQuery(" select min(sort) from s_dictionary_detail where sort in (select sort from s_dictionary_detail where sort> :sort and pid = (select id from s_dictionary_class where code= :code))");
				query.setParameter("sort", sort);
				query.setParameter("code", code);
				
				return query.uniqueResult();
			}
			
		});
	}
	
	public Object findDownSort(final Integer sort, final String  code) 
	{
		return this.getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException
			{
				Query query = session.createSQLQuery(" select max(sort) from s_dictionary_detail where sort in (select sort from s_dictionary_detail where sort< :sort and pid = (select id from s_dictionary_class where code= :code))");
				query.setParameter("sort", sort);
				query.setParameter("code", code);
				
				return query.uniqueResult();
			}
			
		});
	}
	
	public Object findDictDetailByCode(final Integer  sort) 
	{
		return this.getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException
			{
				Query query = session.createQuery(" from SDictionaryDetail where sort=:sort");
				query.setParameter("sort", sort);
				return query.uniqueResult();
			}
			
		});
	}
	
	public void updateSort( SDictionaryDetail sDictionaryDetail)
	{
		this.getHibernateTemplate().update(sDictionaryDetail);
	}
	
	public Object saveDictionaryDetails(Object obj) 
	{
		return this.getHibernateTemplate().save(obj);
	}
	
	public void updateDictionaryDetails(Object obj) 
	{
		this.getHibernateTemplate().update(obj);
	}
	
	public void delDictionaryDetailsById( final String id) 
	{
		this.getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException
			{
				Query query = session.createSQLQuery(" delete from s_dictionary_detail where id=:id");
				query.setParameter("id", id);
				int count = query.executeUpdate();
				return count;
			}
			
		});
	}
	
	public List findDictDetailByCodes(final Integer  sort) 
	{
		return (List)this.getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException
			{
				Query query = session.createQuery(" from SDictionaryDetail where sort=:sort");
				query.setParameter("sort", sort);
				return query.list();
			}
			
		});
	}
}
